package cn.hicard.risk.io;

import java.io.File;
import java.text.DecimalFormat;

public class Fileinfo {
	
	/**
	 * 移动filepath到target<br/>
	 * @param filepath
	 * @param dir
	 */
	public static boolean move(String filepath, String target){
		File file = new File(filepath);
		if(file.exists()){
			File dest = new File(target);
			return file.renameTo(dest);
		}
		return false;
	}

	
	/**
	 * 根据文件的大小以适当的单位显示大小信息
	 * @param file
	 * @return
	 */
	public static String sizeInfo(File file){
		DecimalFormat df = new DecimalFormat("0.00");
		long length = file.length();
		if(length < 1024){
			return length + "B";
		}
		if(length < 1024 * 1024){
			return df.format(length/1024d) + "KB";
		}
		if(length < 1024 * 1024 * 1024){
			return df.format(length/(1024*1024d)) + "MB";
		}
		if(length < 1024 * 1024 * 1024 * 1024){
			return df.format(length/(1024*1024*1024d)) + "GB";
		}
		return df.format(length/(1024*1024*1024*1024d)) + "TB";
	}

}
